<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <!-- 做分页功能引入css和js -->
    <link rel="stylesheet" href="./libs/layui/css/layui.css">
    <script src="./libs/layui/layui.js"></script>
    <script src="./libs/jquery/jquery.min.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <div class="form-inline">
                        <input type="text" id="aname" class="form-control input-sm" placeholder="输入文章标题...">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                            <!-- <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option> -->
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </div>
                </div>
                <div class="col-xs-6">
                    <a href="article_add.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody id="artList_tbody">
                    <!-- <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr> -->
                </tbody>
            </table>
            <div class="row text-center" id="page">
            </div>
        </div>
    </div>

    <script src="./js/http.js"></script>
    <!--导入art-template模板js -->
    <script src="./libs/art-template/template-web.js"></script>

    <!-- 定义类别模板 -->
    <script id="art_category" type="text/html">
        {{each data}}
            <option value="{{$value.id}}">{{$value.name}}</option>
        {{/each}}
    </script>
    <!-- 定义列表数据模板 -->
    <script id="art_list" type="text/html">
        {{each data}}
            <tr>
                <td>{{$value.title}}</td>
                <td>{{$value.author}}</td>
                <td>{{$value.category}}</td>
                <td class="text-center">{{$value.date}}</td>
                <td class="text-center">{{$value.state}}</td>
                <td class="text-center">
                    <a href="article_edit.html?id={{$value.id}}" class="btn btn-default btn-xs">编辑</a>
                    <a href="javascript:void(0);" class="btn btn-danger btn-xs delete" onclick=del({{$value.id}})>删除</a>
                </td>
            </tr>
        {{/each}}
    </script>
    <script>
        $(function () {
            // 获取文章分类
            getCategory();
            // 获取文章列表
            getArticalList();
            // 点击筛选
            $('#btnSearch').click(function () {
                getArticalList();
            })
        })

        // 获取文章类别
        function getCategory() {
            myAjax('GET', '/admin/category/list', {}, function (resData) {
                // console.log('分类', resData);
                // 使用定义好的模板字符串
                let art_categoryHtml = template('art_category', resData);
                $('#selCategory').append(art_categoryHtml);
            })
        }
        // 获取文章列表-搜索
        function getArticalList(currpage = 1, perpage = 6) {
            let params_list = {
                key: $('#aname').val(),
                type: $('#selCategory').val(),
                state: $('#selStatus').val(),
                page: currpage,
                perpage: perpage
            }
            myAjax('GET', '/admin/article/query', params_list, function (resData) {
                // console.log(resData);
                let art_listHtml = template('art_list', resData.data);
                $('#artList_tbody').html(art_listHtml);
                // 将数据总数传递过去，生成分页
                get_page(resData.data.totalCount, currpage);
            })
        }

        // 分页功能
        function get_page(totalCount, currpage) {
            layui.use('laypage', function () {
                var laypage = layui.laypage;

                //执行一个laypage实例
                laypage.render({
                    elem: 'page', //注意，这里的 test1 是 ID，不用加 # 号
                    count: totalCount, //数据总数，从服务端得到
                    // 每次都从头加载，每次都是1，因此这里进行手动传递赋值操作
                    curr: currpage,
                    limit: 6,
                    jump: function (obj, first) {
                        //obj包含了当前分页的所有参数，比如：
                        // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                        // console.log(obj.limit); //得到每页显示的条数

                        //首次不执行,防止互相一直调用产生死循环
                        if (!first) {
                            //do something
                            getArticalList(obj.curr, obj.limit);
                        }
                    }
                });
            });
        }
        // 删除功能
        function del(p_id) {
            myAjax('POST', '/admin/article/delete', { id: p_id }, function (resData) {
                console.log(resData);
                if (resData.code === 200) window.location = './article_list.html';
            })
        }

        $('#release_btn').click(function () {
            $('.level02 li', window.parent.document).eq(1).addClass('active').siblings().removeClass('active');
        })

    </script>
</body>

</html>